﻿using System;
using System.Collections.Generic;
using AutoMapper;
using BestTrader.Cms.IServices;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.ViewModels.WebApi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BestTrader.Cms.WebApi.Controllers.v1
{
    /// <summary>
    /// 返佣记录
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [ApiController]
    public class RebateRecordController : BaseController
    {
        private readonly IRebateRecordService _service;
        private readonly IManagerService _managerService;
        private readonly IMapper _mapper;

        public RebateRecordController(IRebateRecordService service, IManagerService managerService, IMapper mapper)
        {

            _service = service;
            _managerService = managerService;
            _mapper = mapper;
        }

        /// <summary>
        ///  [10001] 获取返佣记录
        ///  
        /// </summary>
        /// <param name="input">返佣记录</param>
        /// <remarks>
        ///OUTPUT: /RebateRecord
        /// {
        ///     "data": {
        ///     "id": 436,
        ///     "managerId": 75,
        ///     "managerName": "bt004",
        ///     "managerRealName": "郑少秋", 
        ///     "parentManagerId": 27,
        ///     "parentManagerName": "BT002",
        ///     "pManagerRealName": "BT002",
        ///     "serviceChargeMoney": 25,   //(手续费 (单边))
        ///     "transactionNo": "2137",   //成交编号
        ///     "transactionTime": "2019-02-18T15:53:45", //成交时间
        ///     "quantity": 1,              //开平数量
        ///     "currency": "USD",          //货币单位
        ///     "projectCode": "EC",        //产品代号
        ///     "projectName": "欧元",      //产品名称
        ///     "remark": "",               //备注
        ///     "rebateMoney": 19,          //返佣金额
        ///     "rebateRMBMoney": 128.16,   //返佣人民币金额
        ///     "addManagerId": null,       //添加人
        ///     "addTime": "2019-02-19T15:28:07.097",
        ///     "modifyManagerId": null,    //修改人
        ///     "modifyTime": null,
        ///     "isDelete": false
        ///     },                      
        ///     "count": "66"             //总记录数
        /// }
        /// </remarks>
        /// <returns></returns>
        /// 
        [HttpPost("GetRebateRecord")]
        [Authorize]
        public JsonResult GetRebateRecord(RebateRecordInput input)
        {
            var model = _mapper.Map<RebateRecordRequestModel>(input);
            if (input.Limit == 0)
            {
                model.Limit = 10;
            }
            if (input.Page == 0)
            {
                model.Page = 1;
            }
            if (input.ManagerId > 0) model.State = input.ManagerId.ToString();

            var result = _service.LoadData(model, input.CurrentId.ToString(), input.RoleId.ToString());
            return Json<Object>(new { data = result.data, count = result.count }, true, 200, result.msg);

        }

        /// <summary>
        /// [10002] 返佣记录的用户列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetAgentName")]
        [Authorize]
        public JsonResult GetAgentName(AgentNameInput input)
        {
            var result = new BaseResult();
            var UserId = input.Uid;
            var RoleId = input.RoleId;

            var data = _managerService.GetSelfAndChildManagerByManagerIdAndRoleId(UserId, RoleId);

            var newData = _mapper.Map<IEnumerable<AgentNameOutput>>(data);

            return Json<IEnumerable<AgentNameOutput>>(newData, result.Success, result.ResultCode, result.ResultMsg);

        }



    }
}
